R Markdown

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Including Plots

You can also embed plots, for example:

zev_sales_one_value <- zev_sales_county %>% 
  pivot_wider(names_from = c(fuel_type, make, model), values_from = number_of_vehicles) %>% 
  filter(data_year == 2022) %>%
  mutate(Total_Sales = rowSums(select(., -c(data_year, county)), na.rm = TRUE))

####
library(plotly)
library(rjson)
## 
## Attaching package: 'rjson'
## The following objects are masked from 'package:jsonlite':
## 
##     fromJSON, toJSON
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)

### add fips to data

urlmap <- "https://raw.githubusercontent.com/kjhealy/fips-codes/master/state_and_county_fips_master.csv"

county_fips_map <- read.csv(urlmap, colClasses=c(fips="character")) %>%
  filter(state == "CA") %>% 
  rename(county = name) %>% 
  mutate(county = ifelse(str_detect(county, " County"), str_replace(county, " County", ""), county))

join_county_fips <- left_join(zev_sales_one_value, county_fips_map, by= "county") %>% 
  drop_na(fips) %>% #out of state
  mutate_at(vars(Total_Sales), as.numeric) %>% 
  #this was really annoying 
  mutate(fips = stringr::str_pad(fips, width = 5, pad = "0")) %>% 
  mutate_at(vars(fips), as.character)%>% 
  mutate_all(~ifelse(is.na(.), 0, .))


### need to check matching--------------------------
g <- list(
  fitbounds = "locations",
  visible = FALSE
)
fig <- plot_ly()
fig <- fig %>% add_trace(
  type="choropleth",
  geojson=counties,
  locations=join_county_fips$fips,
  z=join_county_fips$Total_Sales,
  colorscale="Blues",
  marker=list(line=list(
    width=0)
  )
)
fig <- fig %>% colorbar(title = "Total EV an PHEV Sales")
fig <- fig %>% layout(
  title = "Total EV an PHEV Sales by County"
)

fig <- fig %>% layout(
  geo = g
)

fig

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.